/**
 * Created by Thinkpad on 2016/5/25 0025.
 */

var $table;
$(function () {

    $table = $("#table");

    $("#addModal").on('show.bs.modal', function (event) {
        var $this = $(this);
        $(':input', '#addForm')
            .not(':button, :submit, :reset')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');
    })

    $("#addForm").submit(function () {
        $.post("/strategy.do", $(this).serialize(), function (data) {
            if (data.code == 0) {
                $table.bootstrapTable('refresh', {silent: true});
            } else {
                $alert.find('p').text(data.desc);
                $alert.show();
            }
        }, "json");

        $("#addModal").modal('hide');
        return false;
    });

    $("#delModal").on("show.bs.modal", function (event) {
        var $this = $(this);
        var selections = $table.bootstrapTable('getSelections');
        if(selections == null || selections.length == 0) {
            $this.find('p.lead').text('没有选中任何记录');
            $this.find('button[type="submit"]').attr('disabled', true);
            return;
        }

        var strategys = new Array();
        var ids = new Array();
        for (var i in selections) {
            var row = selections[i];
            ids.push(row.id);
            strategys.push('(' + row.scores + '积分/' + row.rmb + '元)');
        }

        $this.find('input[name="ids"]').val(ids.join());
        $this.find('p.lead').text(strategys.join());
        $this.find('button[type="submit"]').attr('disabled', false);
    });

    $('#delForm').submit(function () {
        $.post("/strategy_del.do", $(this).serialize(), function (data) {
            if (data.code == 0) {
                $table.bootstrapTable('refresh', {silent: true});
            } else {
                $alert.find('p').text(data.desc);
                $alert.show();
            }
        }, "json");

        $("#delModal").modal('hide');

        return false;
    });

});